home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 40
/
Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso
/
Aminet
/
util
/
cdity
/
ModeProSrc.lha
/
Daemon
/
Unnamed1
/
40beta
/
MPMain.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-06-12
|
16KB
|
559 lines
#include "MP.h"
#include <utility/hooks.h>
#include <intuition/sghooks.h>
#include <proto/locale.h>
long __stack = 4096;
UBYTE EnvFile[] ="ENV:ModePro.pref",
EnvArcFile[]="ENVARC:ModePro.pref";
#define MPVER "4.0"
#define VER 4
#define REV 0
UBYTE Version[] ="$VER: ModePro "MPVER" "__AMIGADATE__;
UBYTE ModePro[] ="ModePro "MPVER;
BYTE PublicSignal;
struct Process *MPTask;
struct Screen ASM *(*OldOpenScreen)(REG __a0 struct NewScreen *,
REG __a6 struct IntuitionBase *);
struct Screen ASM *(*OldOpenScreenTagList)(REG __a0 struct ExtNewScreen *ns,
REG __a1 struct TagItem *taglist,
REG __a6 struct IntuitionBase * );
BOOL ASM (*OldCloseScreen)(REG __a0 struct Screen *,
REG __a6 struct IntuitionBase *);
struct Window ASM *(*OldOpenWindow)(REG __a0 struct NewWindow *nw,
REG __a6 struct IntuitionBase *lib);
struct Window ASM *(*OldOpenWindowTagList)(REG __a0 struct NewWindow *nw,
REG __a1 struct TagItem *tags,
REG __a6 struct IntuitionBase *lib);
BOOL ASM *(*OldLayoutMenusA)(REG __a0 struct Menu *fm,
REG __a1 APTR vi,
REG __a2 struct TagItem *tags,
REG __a6 struct Library *lib);
BOOL V39;
ULONG Deinstall=FALSE;
struct NewBroker NewBroker=
{
NB_VERSION,
"ModePro",
ModePro,
0,
NBU_UNIQUE | NBU_NOTIFY,
COF_SHOW_HIDE,
0,0,0
};
UWORD NumDriPens;
UWORD defaultpens[]=
{
0,1,1,2,1,3,1,0,2,1,2,1
};
struct TextAttr Topaz8 = { "topaz.font",8,0,0};
struct MsgPort *BrokerPort,
*CatchPort,
*ReplyPort;
struct Message *ms;
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Library *AslBase,
*CxBase,
*DiskfontBase,
//*GadToolsBase,
*IconBase,
*IFFParseBase,
*LocaleBase,
*UtilityBase;
struct Libs MP_Libs[]=
{
(APTR *)&AslBase, "asl.library", 38,
(APTR *)&CxBase, "commodities.library",36,
(APTR *)&DiskfontBase, "diskfont.library", 36,
// (APTR *)&GadToolsBase, "gadtools.library", 36,
(APTR *)&GfxBase, "graphics.library", 36,
(APTR *)&IconBase, "icon.library", 36,
(APTR *)&IFFParseBase, "iffparse.library", 36,
(APTR *)&IntuitionBase,"intuition.library", 36,
// (APTR *)&LocaleBase, "locale.library", 38,
(APTR *)&UtilityBase, "utility.library", 36,
0,0,0
};
CxObj *Broker;
CxMsg *cxmsg;
ULONG cxsig;
ULONG sigrcvd;
/*** Tooltypes ***/
UBYTE *Hotkey="lcommand p",
Popup=TRUE;
BYTE Priority=0;
//UBYTE *UserFontName=0;
//LONG UserFontSize=8;
STRPTR PrefsPath;
struct Catalog *Catalog;
struct MPSem MPSem;
void main(int argc, char **argv)
{
ULONG l;
struct Screen *ps;
struct DrawInfo *di;
if(LocaleBase=OpenLibrary("locale.library",38))
Catalog=OpenCatalog(NULL,(STRPTR)"modeprodaemon.catalog", TAG_DONE);
else
Catalog=NULL;
InitStrings();
if(OpenLibs(ModePro,MP_Libs))
{
NewBroker.nb_Descr=GetString(MSG_COMMODITY_TITLE);
if(((struct Library *)IntuitionBase)->lib_Version>38)
{
NumDriPens=12;
V39=TRUE;
}
else
{
NumDriPens=9;
V39=FALSE;
}
if(GetArgs(argc,argv))
{
{
struct SignalSemaphore *sem;
sem=&MPSem.SharedSem;
for(l=0;l<SEMAPHORES;l++)
InitSemaphore(&sem[l]);
}
MPSem.SharedSem.ss_Link.ln_Name =(UBYTE *)"ModePro";
MPSem.SharedSem.ss_Link.ln_Pri =0;
MPSem.Enabled =TRUE;
MPSem.Version =VER;
MPSem.Revision=REV;
MPSem.HotKey =Hotkey;
if(ps=LockPubScreen(NULL))
{
di=GetScreenDrawInfo(ps);
for(l=0;l<di->dri_NumPens && l<NumDriPens;l++)
defaultpens[l]=di->dri_Pens[l];
FreeScreenDrawInfo(ps,di);
UnlockPubScreen(NULL,ps);
}
if(BrokerPort=CreateMsgPort())
{
NewBroker.nb_Pri=Priority;
NewBroker.nb_Port=BrokerPort;
if(Broker=CxBroker(&NewBroker,NULL))
{
AddHotKey(Broker,BrokerPort,Hotkey,EVT_HOTKEY);
if(CatchPort=CreateMsgPort())
{
if(MPSem.DaemonPort=CreateMsgPort())
{
if(ReplyPort=CreateMsgPort())
{
if((PublicSignal=AllocSignal(-1))>=0)
{
MPTask=(struct Process *)FindTask(0);
cxsig=1L<<BrokerPort->mp_SigBit;
ActivateCxObj(Broker,1);
NewList(&MPSem.OpenList);
NewList(&MPSem.PromotionList[0]);
NewList(&MPSem.PromotionList[1]);
if(!DaemonLoadFile(EnvFile))
DaemonLoadFile(EnvArcFile);
Forbid();
OldCloseScreen =SetFunction((struct Library *)IntuitionBase,-(0x42) ,(FUNCCAST)NewCloseScreen);
OldOpenScreenTagList =SetFunction((struct Library *)IntuitionBase,-(0x264),(FUNCCAST)NewOpenScreenTagList);
OldOpenScreen =SetFunction((struct Library *)IntuitionBase,-(0xc6) ,(FUNCCAST)NewOpenScreen);
OldOpenWindowTagList =SetFunction((struct Library *)IntuitionBase,-(0x25e),(FUNCCAST)NewOpenWindowTagList);
OldOpenWindow =SetFunction((struct Library *)IntuitionBase,-(0xcc) ,(FUNCCAST)NewOpenWindow);
OldLayoutMenusA =SetFunction((struct Library *)GadToolsBase ,-(0x42) ,(FUNCCAST)NewLayoutMenusA);
Permit();
AddSemaphore((struct SignalSemaphore *)&MPSem);
ProcessInput();
RemSemaphore((struct SignalSemaphore *)&MPSem);
ObtainSemaphore(&MPSem.ListSem);
ReleaseSemaphore(&MPSem.ListSem);
ObtainSemaphore(&MPSem.PrefsSem);
ReleaseSemaphore(&MPSem.PrefsSem);
ProcessMPMessages();
{
struct MPMessage *mpm;
while(mpm=(struct MPMessage *)GetMsg(ReplyPort))
FreeVec(mpm);
}
FreeDefList(&MPSem.PromotionList[0]);
FreeDefList(&MPSem.PromotionList[1]);
FreeSignal(PublicSignal);
}
DeleteMsgPort(ReplyPort);
}
DeleteMsgPort(MPSem.DaemonPort);
}
DeleteMsgPort(CatchPort);
}
DeleteCxObjAll(Broker);
while(cxmsg=(CxMsg *)GetMsg(BrokerPort))
ReplyMsg((struct Message *)cxmsg);
}/* endif CxBroker() */
DeleteMsgPort(BrokerPort);
}// endif CreateMsgPort(BrokerPort)
// ArgArrayDone();
CleanupArgs(argc);
}// endif GetArgs()
CloseLibs(MP_Libs);
}
if(LocaleBase)
{
CloseCatalog(Catalog);
CloseLibrary(LocaleBase);
}
}
void ProcessInput(void)
{
struct DefaultNode *catdnode;
ULONG replysig,portsig,catchsig,signal,msgtype,pubsig,allsigs,daemonsig;
LONG msgid;
daemonsig=1<< MPSem.DaemonPort->mp_SigBit;
catchsig=1 << CatchPort->mp_SigBit;
portsig =1 << BrokerPort->mp_SigBit;
pubsig =1 << PublicSignal;
replysig=1 << ReplyPort->mp_SigBit;
allsigs=daemonsig | replysig | pubsig | catchsig | portsig | SIGBREAKF_CTRL_C;
if(Popup)
ShowGUI();
while (!Deinstall)
{
signal=Wait (allsigs);
if(SIGBREAKF_CTRL_C & signal)
CheckToQuit();
if(daemonsig & signal)
ProcessMPMessages();
if(replysig & signal)
{
struct MPMessage *mpm;
while(mpm=(struct MPMessage *)GetMsg(ReplyPort))
FreeVec(mpm);
}
if(pubsig & signal)
{
SetSignal(0,pubsig);
ClosePubScreens();
}
if(catchsig & signal)
{
while(catdnode=(struct DefaultNode *)GetMsg(CatchPort))
{
AddDNode(catdnode->Type,catdnode);
}
if(MPSem.CatchPopup)
ShowGUI();
}
if(portsig & signal)
{
while(cxmsg=(CxMsg *)GetMsg(BrokerPort))
{
msgid=CxMsgID(cxmsg);
msgtype=CxMsgType(cxmsg);
ReplyMsg((struct Message *)cxmsg);
switch(msgtype)
{
case CXM_IEVENT:
switch(msgid)
{
case EVT_HOTKEY:
ShowGUI();
break;
default:
{
struct OpenNode *on;
ObtainSemaphoreShared(&MPSem.ListSem);
on=(struct OpenNode *)MPSem.OpenList.lh_Head;
while(on->on_Node.ln_Succ)
{
if(on=(struct OpenNode *)msgid)
{
ScreenToFront(on->Screen);
DeleteCxObjAll(on->Cx);
on->Cx=AddHotKey(Broker,BrokerPort,on->HotKey,(ULONG)on);
break;
}
on=(struct OpenNode *)on->on_Node.ln_Succ;
}
ReleaseSemaphore(&MPSem.ListSem);
}
}
break;
case CXM_COMMAND:
switch(msgid)
{
case CXCMD_DISABLE:
MPSem.Enabled=FALSE;
SendPrefsMsg(MP_REFRESH,0,0);
break;
case CXCMD_ENABLE:
MPSem.Enabled=TRUE;
SendPrefsMsg(MP_REFRESH,0,0);
break;
case CXCMD_KILL:
CheckToQuit();
break;
case CXCMD_UNIQUE:
case CXCMD_APPEAR:
ShowGUI();
break;
case CXCMD_DISAPPEAR:
HideGUI();
break;
}
break;
}
}
}
}
HideGUI();
}
void ProcessMPMessages(void)
{
struct MPMessage *mpm;
while(mpm=(struct MPMessage *)GetMsg(MPSem.DaemonPort))
{
//printf("DaemonMsg Command %d Code %d Data %8x\n",mpm->Command,mpm->Code,mpm->Data);
switch(mpm->Command)
{
case MP_QUIT:
CheckToQuit();
break;
/*
case MP_NEWNODE:
AddDNode(mpm->Code,mpm->Data);
break;
case MP_REMNODE:
RemDNode(mpm->Data);
break;
*/
}
ReplyMsg((struct Message *)mpm);
}
}
void ClosePubScreens(void)
{
struct OpenNode *on,*on2;
ObtainSemaphore(&MPSem.OpenListSem);
on=(struct OpenNode *)MPSem.OpenList.lh_Head;
while(on->on_Node.ln_Succ)
{
if(on->Flags & ON_PUBLIC && !(on->Flags & ON_OPEN))
{
if(OldCloseScreen(on->Screen,IntuitionBase))
{
on2=(struct OpenNode *)on->on_Node.ln_Pred;
Remove((struct Node *)on);
FreeOpenNode(on);
on=on2;
}
}
on=(struct OpenNode *)on->on_Node.ln_Succ;
}
ReleaseSemaphore(&MPSem.OpenListSem);
}
void CheckToQuit(void)
{
APTR CheckCloseScreen,
CheckOpenScreen,
CheckOpenScreenTagList,
CheckOpenWindow,
CheckOpenWindowTagList,
CheckLayoutMenusA;
BOOL patchedover=FALSE,
screensopen=FALSE;
ULONG len;
UBYTE *warning;
UBYTE *warn;
ObtainSemaphoreShared(&MPSem.ListSem);
ObtainSemaphore(&MPSem.OpenListSem);
Forbid();
if(IsListEmpty(&MPSem.OpenList))
{
/**** check LVOs and make sure their mine ****/
/**** get LVOs,so put mine in thier, and get the result ****/
CheckCloseScreen =SetFunction((struct Library *)IntuitionBase,-(0x42), (FUNCCAST)OldCloseScreen);
CheckOpenScreenTagList =SetFunction((struct Library *)IntuitionBase,-(0x264),(FUNCCAST)OldOpenScreenTagList);
CheckOpenScreen =SetFunction((struct Library *)IntuitionBase,-(0xc6), (FUNCCAST)OldOpenScreen);
CheckOpenWindowTagList =SetFunction((struct Library *)IntuitionBase,-(0x25e),(FUNCCAST)OldOpenWindowTagList);
CheckOpenWindow =SetFunction((struct Library *)IntuitionBase,-(0xcc), (FUNCCAST)OldOpenWindow);
CheckLayoutMenusA =SetFunction((struct Library *)GadToolsBase ,-(0x42), (FUNCCAST)OldLayoutMenusA);
/**** now restore the LVOs to whatever they were ***/
if((APTR)CheckCloseScreen != (APTR)NewCloseScreen ||
(APTR)CheckOpenScreen != (APTR)NewOpenScreen ||
(APTR)CheckOpenScreenTagList != (APTR)NewOpenScreenTagList ||
(APTR)CheckOpenWindow != (APTR)NewOpenWindow ||
(APTR)CheckOpenWindowTagList != (APTR)NewOpenWindowTagList ||
(APTR)CheckLayoutMenusA != (APTR)NewLayoutMenusA)
{
SetFunction((struct Library *)IntuitionBase,-(0x42), (FUNCCAST)CheckCloseScreen);
SetFunction((struct Library *)IntuitionBase,-(0x264),(FUNCCAST)CheckOpenScreenTagList);
SetFunction((struct Library *)IntuitionBase,-(0xc6) ,(FUNCCAST)CheckOpenScreen);
SetFunction((struct Library *)IntuitionBase,-(0x25e),(FUNCCAST)CheckOpenWindowTagList);
SetFunction((struct Library *)IntuitionBase,-(0xcc) ,(FUNCCAST)CheckOpenWindow);
SetFunction((struct Library *)GadToolsBase ,-(0x42) ,(FUNCCAST)CheckLayoutMenusA);
patchedover=TRUE;
}
else
{
Deinstall=TRUE;
SendPrefsMsg(MP_QUIT,0,0);
}
}
else
{
struct OpenNode *on;
warn=GetString(MSG_REQ_CANT_QUIT_SCREENS);
len=strlen(warn);
on=(struct OpenNode *)MPSem.OpenList.lh_Head;
while(on->on_Node.ln_Succ)
{
len+=strlen(on->Screen->Title)+4;
on=(struct OpenNode *)on->on_Node.ln_Succ;
}
if(warning=AllocVec(len,MEMF_CLEAR|MEMF_NO_EXPUNGE))
{
strcpy(warning,warn);
on=(struct OpenNode *)MPSem.OpenList.lh_Head;
while(on->on_Node.ln_Succ)
{
strcat(warning,"\n ");
strcat(warning,on->Screen->Title);
on=(struct OpenNode *)on->on_Node.ln_Succ;
}
screensopen=TRUE;
}
}
ReleaseSemaphore(&MPSem.OpenListSem);
ReleaseSemaphore(&MPSem.ListSem);
Permit();
if(patchedover)
EZReq(0,0,ModePro,GetString(MSG_REQ_CANT_QUIT_PATCHED_OVER),GetString(MSG_REQ_OK),0);
if(screensopen && warning)
{
EZReq(0,0,ModePro,warning,GetString(MSG_REQ_OK),0);
FreeVec(warning);
}
}
LONG SendPrefsMsg(ULONG Command, ULONG Code, APTR Data)
{
LONG rv=-1;
struct MPMessage *mpm;
ObtainSemaphore(&MPSem.PortSem);
if(MPSem.PrefsPort)
{
rv=0;
if(mpm=AllocVec(sizeof(struct MPMessage),MEMF_PUBLIC|MEMF_CLEAR))
{
mpm->Command=Command;
mpm->Code=Code;
mpm->Data=Data;
PutMsg(MPSem.PrefsPort,(struct Message *)mpm);
rv=1;
}
}
ReleaseSemaphore(&MPSem.PortSem);
return(rv);
}
BOOL DaemonLoadFile(STRPTR FileName)
{
LONG error;
BOOL rv;
// no need for protection, the lists are not public
// when this function is called.
rv=LoadFile(MPSem.PromotionList,FileName);
if(error=IoErr())
{
UBYTE errstr[91];
Fault(error,GetString(MSG_REQ_LOAD_ERROR),errstr,90);
EZReq(0,0,ModePro,errstr,GetString(MSG_REQ_OK),0);
}
return(rv);
}